//
//  YHYStyleViewController.m
//  Home
//
//  Created by 杨泓晔 on 16/4/19.
//  Copyright © 2016年 LanJue. All rights reserved.
//

#import "YHYStyleViewController.h"
#import "YHYJobCollectionCell.h"
#import "YHYmoreHeadCollectionReusableView.h"
#import "DCPaymentView.h"
@interface YHYStyleViewController ()<UICollectionViewDataSource,UICollectionViewDelegate>
{
    NSIndexPath *tempIndexPath;
    
}
@property (nonatomic,retain)UICollectionView *collectionView;
@property (nonatomic,retain)NSMutableArray *array;
@property (nonatomic,retain)NSMutableArray *keyArr;
@property (nonatomic,retain)NSString *insetStr;
@property (nonatomic, strong) NSIndexPath *indexPath;

@end

@implementation YHYStyleViewController

-(void) viewWillAppear:(BOOL)animated
{
    self.tabBarController.tabBar.hidden=YES;
}

-(void) viewWillDisappear:(BOOL)animated
{
    self.tabBarController.tabBar.hidden=NO;
}


- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    [self creatView];
    self.view.backgroundColor = LV;
    _keyArr = [NSMutableArray array];
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"back"] style:UIBarButtonItemStylePlain target:self action:@selector(Back:)];
    self.navigationItem.leftBarButtonItem.tintColor = [UIColor whiteColor];
    
    
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"queren"] style:UIBarButtonItemStylePlain target:self action:@selector(Save:)];
    self.navigationItem.rightBarButtonItem.tintColor = [UIColor whiteColor];
    
    
    self.view.backgroundColor = LV;
}

- (void)creatView{
    
    UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc] init];
    _array = [NSMutableArray arrayWithObjects:@"室内设计",@"工装设计",@"软装设计",@"3D设计",@"建筑设计",@"园林设计", nil];
    // flowLayout.itemSize = CGSizeMake(SCREEN_WIDTH - (14.00 * AUTO_WIDTH * 2), 330.00 * AUTO_WIDTH);
    flowLayout.itemSize = CGSizeMake((SCREEN_WIDTH -  76* AUTO_WIDTH - 10 * 3)/3, 40 * AUTO_HEIGHT);
    flowLayout.sectionInset = UIEdgeInsetsMake(0 * AUTO_HEIGHT, 76/2 * AUTO_WIDTH, 15, 76/2 * AUTO_WIDTH);
    flowLayout.minimumLineSpacing = 12 * AUTO_HEIGHT;
    flowLayout.minimumInteritemSpacing = 10 * AUTO_WIDTH;
    
    self.collectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT) collectionViewLayout:flowLayout];
    self.collectionView.backgroundColor = LV;
    self.collectionView.delegate = self;
    self.collectionView.dataSource = self;
    //设置header大小
    flowLayout.headerReferenceSize  = CGSizeMake(SCREEN_WIDTH, 60* AUTO_HEIGHT);
    
    [self.collectionView registerClass:[YHYJobCollectionCell class] forCellWithReuseIdentifier:@"cell"];
    
    // CollectonView的header  同cell一样遵循重用机制
    [self.collectionView registerClass:[YHYmoreHeadCollectionReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"YHYmoreHeadCollectionReusableView"];

    [self.view addSubview:_collectionView];
    //_collectionView.allowsSelection = YES;
    [_collectionView reloadData];
    
    
}
//增补视图
-(UICollectionReusableView*)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath
{
    YHYmoreHeadCollectionReusableView *reusableView  = [collectionView dequeueReusableSupplementaryViewOfKind:kind withReuseIdentifier:@"YHYmoreHeadCollectionReusableView" forIndexPath:indexPath];

    if (kind == UICollectionElementKindSectionHeader ) {
        reusableView.backgroundColor = LV;
    }
        return reusableView;
}


- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
       return _array.count + 1;
}


- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    
    YHYJobCollectionCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"cell" forIndexPath:indexPath];
    cell.image.hidden = YES;

         if (indexPath.row != _array.count ) {
         cell.contentStr = [_array objectAtIndex:indexPath.row];
             cell.image.hidden = YES;

          if (_keyArr != nil) {
              
              if ([_keyArr containsObject:cell.contentStr]) {
                 
                  [cell changeColor:YES];
              } else {
                  
                  [cell changeColor:NO];
              }
              
          }
          

   }
      else{
          cell.num = 2;
         
             }
    
    
    
    return cell;
    
}


- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
     _indexPath = indexPath;
    YHYJobCollectionCell *cell = (YHYJobCollectionCell *)[collectionView cellForItemAtIndexPath:indexPath];
    if (indexPath.row == _array.count) {
       // [self addItem];
        DCPaymentView *payAlert = [[DCPaymentView alloc] init];
        payAlert.title = [NSString stringWithFormat:@"请输入"];
        payAlert.numOfContent = 7;
        [payAlert show];
        payAlert.completeHandle = ^(NSString *inputPwd) {
            [_array insertObject:inputPwd atIndex:_array.count ];
            
            [self.collectionView reloadSections:[NSIndexSet indexSetWithIndex:_indexPath.section]];
            NSLog(@"last%@",[_array lastObject]);
        };

        
    }
   
    else{
        
     if ([_keyArr containsObject:cell.contentStr]) {
        // [cell changeColor:NO];
        [_keyArr removeObject:cell.contentStr];
        
    }
    else{
        
        if (_keyArr.count < 6) {
            [_keyArr addObject:cell.myLabel.text];
        }
        
        
    }
    if (_keyArr.count >= 6) {
        NSString *title = NSLocalizedString(@"提示", nil);
        NSString *message = NSLocalizedString(@"只能添加六个", nil);
        NSString *cancelButtonTitle = NSLocalizedString(@"取消", nil);
        NSString *otherButtonTitle = NSLocalizedString(@"确认", nil);
        
        UIAlertController *alertController = [UIAlertController alertControllerWithTitle:title message:message preferredStyle:UIAlertControllerStyleAlert];
        
        // Create the actions.
        UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:cancelButtonTitle style:UIAlertActionStyleCancel handler:^(UIAlertAction *action) {
            // NSLog(@"The \"Okay/Cancel\" alert's cancel action occured.");
        }];
        
        UIAlertAction *otherAction = [UIAlertAction actionWithTitle:otherButtonTitle style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
            //  NSLog(@"The \"Okay/Cancel\" alert's other action occured.");
            [self.navigationController popViewControllerAnimated:YES];
            [self.delegate persentStyleNumberArray:_keyArr];
        }];
        
        // Add the actions.
        [alertController addAction:cancelAction];
        [alertController addAction:otherAction];
        
        [self presentViewController:alertController animated:YES completion:nil];
        
        
    }
    
    
    [_collectionView reloadData];
}
      [_collectionView reloadData];
   
}
- (void)addItem {
    
    DCPaymentView *payAlert = [[DCPaymentView alloc] init];
    payAlert.title = [NSString stringWithFormat:@"请输入"];
    payAlert.numOfContent = 7;
    [payAlert show];
    payAlert.completeHandle = ^(NSString *inputPwd) {
        [_array insertObject:inputPwd atIndex:_array.count ];
        
        [self.collectionView reloadSections:[NSIndexSet indexSetWithIndex:_indexPath.section]];
        NSLog(@"last%@",[_array lastObject]);
    };
    // [_collectionView reloadData];
}



- (void)Save:(UIButton*)save{
    [self.navigationController popViewControllerAnimated:YES];
    [self.delegate persentStyleNumberArray:_keyArr];
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

-(void)Back:(UIButton *) goback
{
    [self.navigationController popViewControllerAnimated:YES];
}


/*
 #pragma mark - Navigation
 
 // In a storyboard-based application, you will often want to do a little preparation before navigation
 - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
 // Get the new view controller using [segue destinationViewController].
 // Pass the selected object to the new view controller.
 }
 */
@end


